<!-- 
    头像组件下修改密码功能下
    密码修改页面
 -->
<template>
    <div>
        <el-card class="box-card">
            <template #header>
                <div class="card-header">
                    <span>修改密码</span>
                </div>
            </template>
            <div>
                <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px"
                    class="demo-ruleForm">
                    <el-form-item label="旧密码" prop="oldpassword">
                        <el-input v-model="ruleForm.oldpassword" type="password" autocomplete="off" />
                    </el-form-item>
                    <el-form-item label="新密码" prop="newpassword">
                        <el-input v-model="ruleForm.newpassword" type="password" autocomplete="off" />
                    </el-form-item>
                    <el-form-item label="新密码确认" prop="fixpassword">
                        <el-input v-model="ruleForm.fixpassword" type="password" autocomplete="off" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm(ruleFormRef)">修改</el-button>
                        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
    </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'

const ruleFormRef = ref<FormInstance>()
// 表单验证提示
const validateFixpwd = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('请再次输入新密码'))
    }
}

const validateOldpwd = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请输入旧密码'))
    } else {
        if (ruleForm.newpassword !== '') {
            if (!ruleFormRef.value) return
            ruleFormRef.value.validateField('checkPass', () => null)
        }
        callback()
    }
}

const validateNewpwd = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请重新输入密码'))
    } else if (value !== ruleForm.oldpassword) {
        callback(new Error("新旧密码不匹配"))
    } else {
        callback()
    }
}

// 表单数据
const ruleForm = reactive({
    oldpassword: '',
    newpassword: '',
    fixpassword: '',
})

// 验证规则
const rules = reactive({
    oldpassword: [{ validator: validateOldpwd, trigger: 'blur' }],
    newpassword: [{ validator: validateNewpwd, trigger: 'blur' }],
    fixpassword: [{ validator: validateFixpwd, trigger: 'blur' }],
})
// 修改事件
const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            console.log('submit!')
        } else {
            console.log('error submit!')
            return false
        }
    })
}
// 重置事件
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style lang="less" scoped>
.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: bold;
}

.text {
    font-size: 14px;
}

.item {
    margin-bottom: 18px;
}

.box-card {
    margin: auto;
    width: 600px;
}
</style>
